//
//  JCComposePhotosView.swift
//  SinaWeibo_18
//
//  Created by apple on 15/10/27.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

//最大列数
private let CHILDVIEW_MAXCOL = 3
//子控件间间距
private let CHILDVIEW_MARGIN: CGFloat = 10

class JCComposePhotosView: UIView {
    //初始当前是 删除图片状态
    var addImageTag: Bool = false
    
    var addButtonClickBlock: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = randomColor()
        addSubview(addButton)
    }
    
    //添加图片
    func addImage(image: UIImage){
        
        addImageTag = true
        
        let imageView = JCComposePhotoView(frame: CGRectZero)
        
        imageView.image = image
        //添加到数组   添加按钮的下方   先显示
        insertSubview(imageView, belowSubview: addButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //图片添加按钮点击
    @objc private func addButtonClick(){
        //通知控制器点击了  添加图片按钮
        if let block = addButtonClickBlock {
            block()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //判断添加图片按钮是否隐藏
        addButton.hidden = (subviews.count == 1 || subviews.count == 10)
    
        let childWH = (width - CGFloat(CHILDVIEW_MAXCOL-1)*CHILDVIEW_MARGIN) / CGFloat(CHILDVIEW_MAXCOL)
        for(index,value) in subviews.enumerate() {
            
                        //设置frame
            value.size = CGSizeMake(childWH, childWH)
            
            let col = index % CHILDVIEW_MAXCOL
            let row = index / CHILDVIEW_MAXCOL
            
            func setChildViewXY(){
                value.x = (CHILDVIEW_MARGIN + childWH) * CGFloat(col)
                value.y = (CHILDVIEW_MARGIN + childWH) * CGFloat(row)
            }
            
            func setPhotoViewXY(anim:Bool){
                if anim{
                    
                    //执行动画   (删除图片才执行)
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        setChildViewXY()
                    })
                }else{
                    //不动画
                    setChildViewXY()
                }
            }
            
            //如果是加号按钮  不执行动画
            if value == addButton {
                
                setPhotoViewXY(!addImageTag)
                
                return
            }
            
            setPhotoViewXY(!(value.x == 0 && value.y == 0))
        }
    }
    
    //将要移除子控件
    override func willRemoveSubview(subview: UIView) {
        addImageTag = false
    }
    
    private lazy var addButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "addButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
}
